/*
 * Copyright 2010 www.LongNet.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using LongNet.Web.Context;
using LongNet.Web.Mvc.Utils;

namespace LongNet.Web.Mvc.Processors {

    internal class LayoutProcessor : ProcessorBase {

        public override void Process( ProcessContext context ) {

            MvcEventPublisher.Instance.BeginAddLayout( context.ctx );

            MvcContext ctx = context.ctx;
            ControllerBase controller = context.getController();

            if (controller.utils.isHided( controller.GetType() )) {
                return;
            }

            String actionContent = context.getContent();

            if (controller.LayoutControllerType == null) {

                runCurrentLayout( controller, ctx, context, actionContent );

            }
            else {
                runOtherLayout( controller, ctx, context, actionContent );

            }

        }

        private void runCurrentLayout( ControllerBase controller, MvcContext ctx, ProcessContext context, String actionContent ) {

            MethodInfo layoutMethod = controller.utils.getMethod( "Layout" );
            if (layoutMethod.DeclaringType != controller.GetType()) {
                String filePath = MvcUtil.getParentViewPath( layoutMethod, ctx.route.getRootNamespace() );
                controller.utils.setCurrentView( controller.utils.getTemplateByFileName( filePath ) );
            }
            else {
                controller.utils.switchViewToLayout();
            }

            controller.actionContent( "" ); // 清理当前内容，否则下面的utils.getActionResult()得不到正确结果
            controller.Layout();

            // 如果showEnd，则跳过combinePage
            if (ctx.utils.isEnd()) {

                context.endMsg( controller.utils.getActionResult() );

            }
            else {
                context.setContent( HtmlCombiner.combinePage( controller.utils.getActionResult(), actionContent ) );
            }

        }

        private void runOtherLayout( ControllerBase controller, MvcContext ctx, ProcessContext context, String actionContent ) {

            ControllerBase layoutController = ControllerFactory.FindController( controller.LayoutControllerType, ctx );
            layoutController.utils.switchViewToLayout();

            ActionRunner.runLayoutAction( ctx, layoutController, layoutController.Layout );


            if (ctx.utils.isEnd()) {
                context.endMsg( layoutController.utils.getActionResult() );
            }
            else {
                context.setContent( HtmlCombiner.combinePage( layoutController.utils.getActionResult(), actionContent ) );
            }

        }


    }

}
